#!/usr/bin/python
#-*- coding: utf-8 -*-

from core.AbstractChecker import AbstractChecker
from tools import *

PACKAGE_PREFIX = 'package'
PACKAGE_NAME_KEY = 'name'
PACKAGE_VERSIONCODE_KEY = 'versionCode'
PACKAGE_VERSIONNAME_KEY = 'versionName'
PACKAGE_LABLE_KEY = 'application-label'
PACKAGE_LABLE_ZH_CN_KEY = 'application-label-zh-CN'

class ApkinfoChecker(AbstractChecker):
    def __init__(self, aapt):
        self.aapt = aapt


    def check(self, filePath, report):
        infos = Utils.execShell('%s dump badging %s' % (self.aapt, filePath))
        _infos = []
        for line in infos:
            _infos.append(line.strip('\r\n'))

        packageName = ''
        versionCode = ''
        versionName = ''
        labelName = ''
        for line in infos:  #按行遍历
            kv = line.split(':')
            if PACKAGE_PREFIX == kv[0]:
                values = kv[1].split(' ')
                for value in values:
                    kv = value.split('=')
                    if PACKAGE_NAME_KEY == kv[0]:
                        packageName = kv[1].replace('\'','')
                    elif PACKAGE_VERSIONCODE_KEY == kv[0]:
                        versionCode = kv[1].replace('\'','')
                    elif PACKAGE_VERSIONNAME_KEY == kv[0]:
                        versionName = kv[1].replace('\'','')
            elif PACKAGE_LABLE_ZH_CN_KEY == kv[0]:
                labelName = kv[1].replace('\'','')
            elif PACKAGE_LABLE_KEY == kv[0] and labelName.strip() == '':
                labelName = kv[1].replace('\'','')
        report.addReport(report.setHeader(3,'packageName: %s' % packageName))
        report.addReport(report.setHeader(3,'labelName: %s' % labelName))
        report.addReport(report.setHeader(3,'versionCode: %s' % versionCode))
        report.addReport(report.setHeader(3,'versionName: %s' % versionName))
        return report